package io.renren.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期处理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月21日 下午12:53:33
 */
public class DateUtils {
	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    // 日期格式化
    private static DateFormat dateFormat = null;
     // 时间格式化
    private static DateFormat dateTimeFormat = null;

    static {
        dateFormat = new SimpleDateFormat(DATE_PATTERN);
        dateTimeFormat = new SimpleDateFormat(DATE_TIME_PATTERN);


    }

	public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if(date != null){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

     /**
     * 日期格式化
     *
     * @param date
     * @return
     */
     public static Date getDateFormat(String date) throws Exception {
                 return dateFormat.parse(date);
     }

     /**
      * 时间格式化
      *
      * @param date
      * @return
      */
     public static Date getDateTimeFormat(String date) throws Exception{
                return dateTimeFormat.parse(date);
     }

    /**
     * 算出还剩多少个月
     *不足一个月按一个月算
     * @param date
     * @return
     */
    public static  int joinMonthsContain(Date befdate,Date aftdate) throws Exception{
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(befdate);
        aft.setTime(aftdate);
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        System.out.println(result);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        System.out.println(month);
        int temp = 0;
        if((aft.get(Calendar.DAY_OF_MONTH) - bef.get(Calendar.DAY_OF_MONTH))>-1){
            temp = 1;
        }
        return Math.abs(month + result ) + temp;
    }
    /**
     * 算出还剩多少个月
     *满足一个月才算一个月
     * @param date
     * @return
     */
    public static  int joinMonths(Date befdate,Date aftdate) throws Exception{
        GregorianCalendar ca = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(befdate);
        aft.setTime(aftdate);
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        System.out.println(result);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        System.out.println(month);
        int temp = 0;
        if((aft.get(Calendar.DAY_OF_MONTH) - bef.get(Calendar.DAY_OF_MONTH))<-1){
            temp = -1;
        }
        return Math.abs(month + result ) + temp;
    }

    /**
     * 获取当前年
     *
     * @param date
     * @return
     */
    public static  String getYear(){
        return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
    }
}
